package servicesImp.business;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.business.BusinessInfoServices;
import services.business.InspectorAssessServices;
import wt.dao.business.InspectorAssessMapper;
import wt.entity.business.BusinessInfo;
import wt.entity.business.InspectorAssess;
import wt.entity.business.InspectorAssessExample;
import wt.staticData.TableName;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class InspectorAssessServicesImp extends IBaseServicesWithoutBLOBsImpl< InspectorAssess, InspectorAssessExample> implements InspectorAssessServices {
    @Autowired
    InspectorAssessMapper inspectorAssessMapper;
    @Autowired
    BusinessInfoServices businessInfoServices;

    @Override
    public IBaseMapperWithoutBLOBs<InspectorAssess, InspectorAssessExample> getMapper() {
        return this.inspectorAssessMapper;
    }

    @Override
    public String getTableName() {
        return TableName.BUS_INSPECTOR_ASSESS;
    }

    @Transactional
    public void insertSelective(List<InspectorAssess> inspectorAssessList, String businessId) throws Exception {
        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);
        businessInfo.setInspectorAssessCount(businessInfo.getInspectorAssessCount() + inspectorAssessList.size());
        businessInfoServices.updateByPrimaryKeySelective(businessInfo);

        for (InspectorAssess inspectorAssess : inspectorAssessList) {
            inspectorAssess.setBusinessId(businessId);
            getCurrentProxy().insertSelective(inspectorAssess);
        }
    }

    public void lazyLoadBusinessInfo(List<InspectorAssess> inspectorAssessList) throws Exception {
        Map<String, BusinessInfo> map = new HashMap<String, BusinessInfo>();
        for (InspectorAssess inspectorAssess : inspectorAssessList) {
            if (!map.containsKey(inspectorAssess.getBusinessId())) {
                map.put(inspectorAssess.getBusinessId(), businessInfoServices.selectByPrimaryKey(inspectorAssess.getBusinessId()));
            }
            inspectorAssess.setBusinessInfo(map.get(inspectorAssess.getBusinessId()));
        }
    }



    public List<InspectorAssess> selectInspectorAssessHistory(Date checkStartTime, Date checkEndTime, String companyName, String productName, String fromInspectorName, String expertId, String toInspectorName) throws Exception {
        return inspectorAssessMapper.selectInspectorAssessHistory(checkStartTime, checkEndTime, companyName, productName, fromInspectorName, expertId, toInspectorName);
    }
}
